package wang.controller;

import wang.bean.Admin;
import wang.dao.AdminDao;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import java.io.IOException;
import java.io.PrintWriter;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

@WebServlet(name = "AdminController", value = "*.admin")
public class AdminController extends HttpServlet {
  @Override
  protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
    PrintWriter out = response.getWriter();
    HttpSession session = request.getSession();
    // 编写正则表达式，匹配以 / 开头，后面跟任意数量的任意字符（除了换行符），最后是 .admin
    String regex = "/(\\w+)\\.admin";
    // 编译正则表达式，创建Matcher对象
    String servletPath = request.getServletPath();
    Matcher matcher = Pattern.compile(regex).matcher(servletPath);
    if (!matcher.matches()) {
      return;
    }
    String requestUrl = matcher.group(1);
    AdminDao adminDao = new AdminDao();
    switch (requestUrl) {
      case "login": {
        String username = request.getParameter("username");
        String secretKey = request.getParameter("secretKey");
        Admin target = adminDao.findByUsername(username);
        if (target == null) {
          out.println("<script>alert('账号不存在');</script>");
          response.setHeader("Refresh", "3,url='login.jsp'");
        } else {
          if (target.getSecretKey().equals(secretKey)) {
            // 密码正确，登录成功
            session.setAttribute("admin", target);
            out.println("<script>location.href='admin_tourist_attraction.jsp';</script>");
          } else {
            out.println("<script>alert('密码错误');</script>");
            response.setHeader("Refresh", "3,url='login.jsp'");
          }
        }
        break;
      }
      case "register": {
        String username = request.getParameter("username");
        String secretKey = request.getParameter("secretKey");
        String name = request.getParameter("name");
        Admin admin = adminDao.findByUsername(username);
        // 如果不为空，代表管理员账号已存在，则跳转到登录页
        if (admin != null) {
          out.println("账号已存在，换个账号吧……");
          response.setHeader("Refresh", "1,url='login.jsp'");
        } else {
          // 如果注册的时候管理员不存在，则保存管理员信息到数据库
          admin = new Admin();
          admin.setUsername(username);
          admin.setSecretKey(secretKey);
          admin.setName(name);
          adminDao.insert(admin);
          out.println("已注册成功，3秒后自动跳转到登录页面……");
          response.setHeader("Refresh", "3,url='login.jsp'");
        }
        break;
      }
      case "logout":
        // 注销登录，销毁session
        request.getSession().invalidate();
        response.sendRedirect("entrance.jsp");
        break;
    }
  }

  @Override
  protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
    this.doGet(request, response);
  }
}
